<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap-v4.6.0.css" />
    <link rel="stylesheet" href="./login.css" />
  </head>

  <body>
    <div class="login-box">
      <div class="form-group">
        <label for="username">账户</label>
        <!-- 账号 -->
        <input
          type="text"
          class="form-control"
          id="username"
          autocomplete="off"
        />
        <small id="emailHelp" class="form-text text-muted"
          >可用帐户：<strong>admin</strong></small
        >
      </div>
      <div class="form-group">
        <!-- 密码 -->
        <label for="password">密码</label>
        <input type="password" class="form-control" id="password" />
        <small id="emailHelp" class="form-text text-muted"
          >可用密码：<strong>123456</strong></small
        >
      </div>
      <button type="submit" class="btn btn-primary" id="btnLogin">登录</button>
    </div>
    <script src="./axios.js"></script>
    <script>
      const usernameId = document.querySelector('#username')
      const passwordId = document.querySelector('#password')
      const but = document.querySelector('#btnLogin')
      but.addEventListener('click', function () {
        const username = usernameId.value
        const password = passwordId.value
        axios({
          url: 'http://ajax-api.itheima.net/api/login',
          method: 'POST',
          data: { username, password },
        })
          .then(({ data: res }) => {
            alert(`${res.message}
          用户名为：${res.data.account}`)
          })
          .catch((error) => {
            alert('账号错误，请重新输入！')
          })
      })
    </script>
  </body>
</html>
